package com.exam.api.modules.system.controller.manager;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.exam.api.common.enums.LoginDevice;
import com.exam.api.common.web.TinyResponse;
import com.exam.api.modules.system.dto.LoginPasswordDTO;
import com.exam.api.modules.system.dto.LoginSmsDTO;
import com.exam.api.modules.system.dto.SmsCodeDTO;
import com.exam.api.modules.system.service.LoginService;
import com.exam.api.modules.system.service.TblUserService;
import com.exam.api.modules.system.vo.LoginUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.exam.api.common.Const.SMS_CODE_KEY;


/**
 * @author iaa
 * @Description
 * @Date 2025/6/17 10:45
 */
@Tag(name = "(管理端) 登录相关接口", description = "(管理端) 登录相关接口")
@RestController
@RequestMapping("/manager")
@RequiredArgsConstructor
@TinyResponse
public class LoginManagerController {

    @Resource
    private LoginService loginService;
    @Resource
    private TblUserService tblUserService;

    @Operation(summary = "pc账号密码登入登录", description = "登录")
    @PostMapping("/login")
    public SaTokenInfo login(@RequestBody @Validated LoginPasswordDTO dto, HttpServletRequest request) {
        return loginService.login(LoginDevice.PC, dto, request);
    }

//    @Operation(summary = "app账号密码登入登录", description = "登录")
//    @PostMapping("/login/app")
//    public SaTokenInfo loginApp(@RequestBody @Validated LoginDTO dto, HttpServletRequest request) {
//        return loginService.login(LoginDevice.APP, dto, request);
//    }

    @Operation(summary = "发送短信验证码", description = "向指定手机号发送验证码")
    @PostMapping("login/sendCode")
    public void sendSmsCode(@RequestBody @Validated SmsCodeDTO dto) {
        loginService.sendSmsCode(dto.getTel(), SMS_CODE_KEY);
    }

    @Operation(summary = "app短信登入", description = "登录")
    @PostMapping("/login/app/phone")
    public SaTokenInfo loginAppPhone(@RequestBody @Validated LoginSmsDTO dto) {
        return loginService.loginAppPhone(dto);
    }

    @Operation(summary = "获取当前登录信息", description = "获取当前登录信息")
    @TinyResponse
    @PostMapping("getInfo")
    public LoginUserInfo getInfo() {
        return tblUserService.getLoginUser();
    }

    @Operation(summary = "注销", description = "注销")
    @PostMapping("/logout")
    public Object logoutSysUser() {
        return loginService.logoutManager();
    }
}
